package cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.service.shopguide;

import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.util.ShopQrCodeUtil;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.controller.admin.shopguide.vo.ShopGuideExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.controller.admin.shopguide.vo.ShopGuidePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.dataobject.shopguide.ShopGuideDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.shopGuide.dal.mysql.shopguide.ShopGuideMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.io.IOException;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 导购 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class ShopGuideServiceImpl implements ShopGuideService {

    @Resource
    private ShopGuideMapper shopGuideMapper;

    @Override
    public Integer createShopGuide(ShopGuideDO shopGuide) throws IOException {
        // 插入
        shopGuideMapper.insert(shopGuide);

        String url = ShopQrCodeUtil.getWXCode(shopGuide.getId().toString());
        shopGuide.setUrl(url);
        shopGuideMapper.updateById(shopGuide);

        // 返回
        return shopGuide.getId();
    }

    @Override
    public void updateShopGuide(ShopGuideDO updateReqVO) {
        // 校验存在
        validateShopGuideExists(updateReqVO.getId());
        // 更新
        shopGuideMapper.updateById(updateReqVO);
    }

    @Override
    public void deleteShopGuide(Integer id) {
        // 校验存在
        validateShopGuideExists(id);
        // 删除
        shopGuideMapper.deleteById(id);
    }

    private void validateShopGuideExists(Integer id) {
        if (shopGuideMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public ShopGuideDO getShopGuide(Integer id) {
        return shopGuideMapper.selectById(id);
    }

    @Override
    public List<ShopGuideDO> getShopGuideList(Collection<Integer> ids) {
        return shopGuideMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopGuideDO> getShopGuidePage(ShopGuidePageReqVO pageReqVO) {
        return shopGuideMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopGuideDO> getShopGuideList(ShopGuideExportReqVO exportReqVO) {
        return shopGuideMapper.selectList(exportReqVO);
    }

}
